package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kittlen.managementSystem.entity.*;
import com.kittlen.managementSystem.service.IEvaluateService;
import com.kittlen.managementSystem.utils.IdWorker;
import com.kittlen.managementSystem.utils.ObjectUtil;
import com.kittlen.managementSystem.utils.TimeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  学员评价  前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/evaluate")
public class EvaluateController {

    @Resource
    IEvaluateService iEvaluateService;

    IdWorker idWorker=new IdWorker(1,1,1);

    /**
     * 评论查询
     * @param evaluate
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selects")
    public Result selects(Evaluate evaluate, @RequestParam(required = false,defaultValue = "0")int page,
                          @RequestParam(required = false,defaultValue = "")String studentId,
                          @RequestParam(required = false,defaultValue = "")String coachId,
                          @RequestParam(required = false,defaultValue = "0")int limit,
                          @RequestParam(required = false,defaultValue = "-1")int type){
        Result result=new Result();
        QueryWrapper<Evaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("score");
        if(ObjectUtil.isNoEmpty(evaluate.getEvaluateId())){
            queryWrapper.eq("evaluate_id", evaluate.getEvaluateId());
        }
        if(ObjectUtil.isNoEmpty(studentId)){
            queryWrapper.eq("t_evaluate.student_id", studentId);
        }
        if(ObjectUtil.isNoEmpty(coachId)){
            queryWrapper.eq("coach_id", coachId);
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        if(type==1){//时间最近
            queryWrapper.orderByDesc("create_date");
        }else if(type==2){//评分最高
            queryWrapper.orderByDesc("score");
        }else if(type==3){//评分最低
            queryWrapper.orderByAsc("score");
        }
        List<Evaluate> list=iEvaluateService.select(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(iEvaluateService.count(queryWrapper));
        return result;
    }

    /**
     * 评价添加
     * @param evaluate
     * @param coachId 教练id
     * @param choiceId 选择id
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/insert")
    public Result insert(Evaluate evaluate,@RequestParam(required = false,defaultValue = "")String coachId,@RequestParam(required = false,defaultValue = "")String choiceId, HttpServletRequest request) throws Exception {
        Result result = new Result();
        if(ObjectUtil.isNoEmpty(choiceId)){
            evaluate.setChoice(new Choice(choiceId));
        }else{
            result.setCode(result.RESULT_ERROR);
            result.setMsg("被评价的教练信息异常,请重试");
            return result;
        }
        if(ObjectUtil.isNoEmpty(coachId)){
            evaluate.setCoach(new Coach(coachId));
        }else{
            result.setCode(result.RESULT_ERROR);
            result.setMsg("被评价的教练信息异常,请重试");
            return result;
        }
        try {
            Student student= (Student) SessionController.getSession(request,"user");
            evaluate.setStudent(student);
        }catch (ClassCastException e){
            result.setCode(result.RESULT_ERROR);
            result.setMsg("用户信息错误,请重新登陆");
            return result;
        }

        evaluate.setEvaluateId(idWorker.nextId()+"");
        if (iEvaluateService.insert(evaluate)>0) {
            result.setData(evaluate);
            result.setMsg("评价成功");
        } else {
            result.setCode(result.RESULT_ERROR);
            result.setMsg("评价失败");
        }
        return result;
    }

    /**
     * 回复评论
     * @param evaluate
     * @param request
     * @return
     */
    @RequestMapping("/reply")
    public Result reply(Evaluate evaluate,HttpServletRequest request) {
        Result result = new Result();
        Coach coach = (Coach) SessionController.getSession(request, "user");
        if(coach==null){
            result.setCode(result.RESULT_ERROR);
            result.setMsg("登录信息超时,请重新登陆");
        }
        boolean update = iEvaluateService.update(new UpdateWrapper<Evaluate>().set("reply", evaluate.getReply()).set("reply_date", TimeUtil.getNowDate()).eq("evaluate_id", evaluate.getEvaluateId()));
        if (update) {
            result.setData(evaluate);
            result.setMsg("回复评价成功");
        } else {
            result.setCode(result.RESULT_ERROR);
            result.setMsg("回复评论失败");
        }
        return result;
    }
}
